package com.zoho.daliansheng.controller;

import com.alibaba.fastjson.JSONObject;
import com.zoho.daliansheng.service.SaleOrderService;
import com.zoho.daliansheng.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/saleOrder")
@CrossOrigin
@Slf4j
public class SaleOrderController {

    SaleOrderService saleOrderService;
    private static final ReentrantLock lock = new ReentrantLock();

    public SaleOrderController(SaleOrderService saleOrderService) {
        this.saleOrderService = saleOrderService;
    }

    @PostMapping("/pushSaleOrderToERP")
    public ResultUtil<Object> pushSaleOrderToERP(@RequestBody JSONObject json) {
        lock.lock();
        log.info("======开始加锁=====");
        try {
            return saleOrderService.pushSaleOrderToERP(json);
        } finally {
            lock.unlock();
            log.info("======已解锁=====");
        }
    }

    @PostMapping("/deleteSaleOrder")
    public ResultUtil<Object> deleteSaleOrder(@RequestBody JSONObject json) {
        return saleOrderService.deleteSaleOrder(json);
    }

}
